package com.itouch.bracelet.activity;

import io.rong.imkit.veiw.SwitchGroup;

import org.json.JSONObject;

import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.itouch.bracelet.R;
import com.itouch.bracelet.bean.BaseResult;
import com.itouch.bracelet.diseaseinfo.Constants;
import com.itouch.util.PatternUtil;

public class LostPSActivity extends BaseActivity implements OnClickListener {

	private EditText lPhone;
	private EditText lVaildcode;
	private EditText lPassword;
	private EditText lRepassword;
	private TextView login;
	private TextView register;
	private TextView lostPs;
	
	@Override
	protected int setContentViewById() {
		return R.layout.activity_lostps;
	}

	@Override
	protected void initViews() {
			lPhone = (EditText)findViewById( R.id.l_phone );
			lVaildcode = (EditText)findViewById( R.id.l_vaildcode );
			lPassword = (EditText)findViewById( R.id.l_password );
			lRepassword = (EditText)findViewById( R.id.l_repassword );
			login = (TextView)findViewById( R.id.login );
			register = (TextView)findViewById( R.id.register );
			lostPs = (TextView)findViewById( R.id.lost_ps );
			login .setOnClickListener(this);
			register. setOnClickListener(this);
			lostPs.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		Intent it=null;
		switch(v.getId())
		{
		case R.id.login:
		
			String phoneString =	lPhone .getText().toString();
			String codeString =		lVaildcode .getText().toString();
			String passwordString =		lPassword.getText().toString();
			String repasswordString =			lRepassword .getText().toString();
			
			if(phoneString==null||!PatternUtil.isPhone(phoneString)){
				Toast.makeText(context, R.string.change_phone,Toast.LENGTH_LONG).show();
				return;
			}	
			
			if(codeString==null||codeString.length()<4){
				Toast.makeText(context, R.string.change_code,Toast.LENGTH_LONG).show();
				return;
			}	

			if(passwordString==null||passwordString.length()<5){
				Toast.makeText(context, R.string.change_pass,Toast.LENGTH_LONG).show();
				return;
			}	
			
			if(repasswordString==null||repasswordString.length()<5){
				Toast.makeText(context, R.string.change_repass,Toast.LENGTH_LONG).show();
				return;
			}	
			
			if(!passwordString.equals(repasswordString)){
				Toast.makeText(context, R.string.change_equale,Toast.LENGTH_LONG).show();
				return;
			}
			
			changePassword(phoneString,passwordString,codeString);
			
			break;
		case R.id.register:
			this.finish();
			
			
			break;
		case R.id.lost_ps:
			it = new Intent(context, RegisterActivity.class);
			startActivity(it);
			this.finish();
			
			
			break;

		default:
			break;

		}
	}
	
	
	

	@Override
	protected void initData() {

	}


	/**
	 * 修改密码
	 * @param password
	 */
	private void changePassword(String phone,String password,String vaildcode) {
	
		show();
		Uri.Builder builder = Uri.parse(Constants.LOST_PASSWORD)
				.buildUpon();
	//	/mobi/customer/updatePwd?phone=15267875837&validCode=123456&password=111111
		builder.appendQueryParameter("validCode",vaildcode);
		builder.appendQueryParameter("phone",phone);
		builder.appendQueryParameter("password",  password );
		Log.d("UPDATE_PASSWORD",
				"UPDATE_PASSWORD" + builder.toString());
		getJSONByVolleyGET(builder.toString(), 
				new Response.Listener<JSONObject>() {

					@Override
					public void onResponse(JSONObject arg0) {
						disMiss();
						Log.d("UPDATE_PASSWORD",
								"UPDATE_PASSWORD" + arg0.toString());
						try {
							BaseResult a = new Gson().fromJson(
									arg0.toString(), BaseResult.class);
							if (a == null) {
								return;
							}
							if (a.isFlag()) {
								Toast.makeText(context,
										R.string.change_pwd_ok,
										Toast.LENGTH_LONG).show();
								LostPSActivity.this.finish();
							} else {
								Toast.makeText(context,
										a.getMsg() + "",
										Toast.LENGTH_LONG).show();
							}
						} catch (Exception e) {
						}
					}

				}, new Response.ErrorListener() {

					@Override
					public void onErrorResponse(VolleyError arg0) {
						disMiss();
						Toast.makeText(context,
								getString(R.string.net_error) + "",
								Toast.LENGTH_LONG).show();
					}
				});

	}

	
}
